<template>
  <div class="main_con">
    <div style="text-align: left;">
      <H2>教师排班日志</H2>
      <el-divider></el-divider>
    </div>

    <el-calendar>
      <template slot="dateCell" slot-scope="{date, data}">
        {{ data.day.split('-').slice(1).join('-') }}
        <el-button
          type="success"
          plain
          size="mini"
          @click="handleAdd(date)"
        >排班</el-button>
        <div  style="width:100%;" v-for="(item,index) in scheduleData" :key="index">
          <el-tag  v-if="(item.workingDay.split(' ').slice(0,1)[0].split('-').slice(1,3).join('-')).indexOf(data.day.split('-').slice(1).join('-'))!=-1">
            {{item.name}}
          </el-tag>
        </div>
      </template>
    </el-calendar>

    <!-- 添加排班对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="考勤组" prop="account">
          <el-select v-model="form.groupId"
                     @change="getClassesAndTeachers(form.groupId)"
                     placeholder="请选择考勤组">
            <el-option v-for="item in attendances"

                       :label="item.groupName"
                       :value="item.groupId"
                       :key="item.groupId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班次选择" prop="groupLeaderId">
          <el-select v-model="form.classId" placeholder="请选择班次">
            <el-option v-for="item in classes"
                       :label="item.className"
                       :value="item.classId"
                       :key="item.classId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="老师选择" prop="groupLeaderId">
          <el-select v-model="form.userId" placeholder="请选择老师">
            <el-option v-for="item in teacheres"
                       :label="item.userName"
                       :value="item.userId"
                       :key="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTeachers, getTeachers, delTeachers, addWorking, updateTeachers, getAllWokings ,getattendances,Classes,Teachers } from "@/api/teacher/teachers";
import { listUser, getUser, delUser, addUser, updateUser, resetUserPwd, changeUserStatus, deptTreeSelect } from "@/api/system/user";
export default {
  name: "Teachers",
  dicts: ['tea_subject_type', 'tea_teacher_type','stu_class_address'],
  data() {
    return {
      value: new Date(),
      scheduleData: [],
      //老师集合
      teacheres: [],
      //考勤组
      attendances:[],
      //班次
      classes: [],
      //所有账号
      Users: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //组长表格数据
      groupTeachersList: [],
      // 教师表表格数据
      teachersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherName: null,
        subject: null,
        teacherPhone: null,
        priority: null,
        teacherType: null,
        teacherSex: null,
        groupLeaderId: null,
        account: null,
        performanceType: null,
        teacherSchool:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getWokings();
  },
  methods: {
    /**查询班次和考勤组成员*/
    getClassesAndTeachers(groupId){
        Classes(groupId).then(res=>{
        this.classes= res.data;
      });
      Teachers(groupId).then(res=>{
        this.teacheres= res.data;
      });
    },
    /**查询所有考勤组*/
    getAttendances(){
      getattendances().then(res=>{
        this.attendances= res.data;
      });
    },
    getWokings(){
      getAllWokings().then(response => {
        this.scheduleData = response.data;
      });
    },
    /**查询所有组长老师*/
    getGroupTeachersList(){
      listTeachers(null).then(response => {
        this.groupTeachersList = response.rows;
      });
    },
    /**查询所有账号*/
    getUsers(){
      listUser(null).then(response => {
        this.Users = response.rows;
      });
    },
    /** 查询教师表列表 */
    getList() {
      this.loading = true;
      listTeachers(this.queryParams).then(response => {
        this.teachersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        teacherId: null,
        teacherName: null,
        subject: null,
        teacherPhone: null,
        priority: null,
        teacherType: null,
        teacherSex: null,
        groupLeaderId: null,
        account: null,
        performanceType: null,
        teacherSchool:null,
        attendances:null,
        teacheres:null

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.teacherId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 排班按钮操作 */
    handleAdd(date) {
      this.reset();
      this.getAttendances();
      alert(date);
      this.form.day=date;
      this.open = true;
      this.title = "添加排班";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getGroupTeachersList();
      this.getUsers();
      const teacherId = row.teacherId || this.ids
      getTeachers(teacherId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改教师表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        addWorking(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getWokings();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const teacherIds = row.teacherId || this.ids;
      this.$modal.confirm('是否确认删除教师表编号为"' + teacherIds + '"的数据项？').then(function() {
        return delTeachers(teacherIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teacher/teachers/export', {
        ...this.queryParams
      }, `teachers_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
